package com.shouhuobao.bhi.collectpoint;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.baidu.location.BDLocation;
import com.collectplus.express.BaseActivityWithSearch;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppConfig;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.SerachStationBean;
import com.collectplus.express.model.ServicePointBean;
import com.shouhuobao.bhi.loc.BaiduLocationService;

import java.util.ArrayList;

import droid.frame.app.Callback;
import droid.frame.umeng.UMeng;

/**
 * 服务网点 、列表模式<br>
 *
 * @author coffee<br>
 * 2016-12-5下午1:49:00
 */
public class ServicePointListActivity extends BaseActivityWithSearch {

    private ListView mListView;
    private ServicePointAdapter mAdapter;
    private ImageView mServicePointIV;

    private Intent baiduLocationService;

    @Override
    @SuppressLint("ResourceAsColor")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showLoadingDialog("正在获取位置");
        baiduLocationService = new Intent();
        baiduLocationService.setClass(getContext(), BaiduLocationService.class);
        baiduLocationService.putExtra("msgId", MsgID.location_service_point);
        startService(baiduLocationService);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.service_point_list);
        super.findViewById();
        getAppTitle().setCommonTitle("投递点");
        mServicePointIV = findViewById(R.id.service_point_banner_iv);
        mListView = (ListView) findViewById(R.id.listview);
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mAdapter.isEmpty()) {
                    return;
                }
                UMeng.click(getContext(), "servicePoint_goto_detail_click");
                ServicePointBean item = (ServicePointBean) parent.getItemAtPosition(position);
                Intent intent = new Intent();
                intent.setClass(getContext(), ServicePointDetailsActivity.class);
                intent.putExtra("item", item);
                startActivity(intent);
            }
        });
        //
        setSearchCallback(new Callback() {
            @Override
            public void execute(Object... result) {
                SerachStationBean.ResultsBean item = (SerachStationBean.ResultsBean) result[0];
                UMeng.click(getContext(), "servicePoint_serach_click");
                showLoadingDialog("查询中");
                AppHttp.getInstance().servicePointSearchByLocation(item.getLocation().getLng(), item.getLocation().getLat());
            }
        });
        //
        final ImageView favorite = (ImageView) findViewById(R.id.service_point_favorite);
        favorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityWhenLogin(ServicePointFavoriteActivity.class);
            }
        });
        // 地图模式
        final ImageView mode = (ImageView) findViewById(R.id.service_point_mode);
        mode.setImageResource(R.drawable.service_point_map);
        mode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getContext(), ServicePointMapActivity.class);
                intent.putExtra("items", mAdapter.getItems());
                startActivityForResult(intent, 101);
            }
        });
        // 切换搜索和非搜索视图
        setSearchSwitch(new Callback() {
            @Override
            public void execute(Object... result) {
                // 非搜索模式
                if (Boolean.FALSE.equals(result[0])) {
                    favorite.setVisibility(View.VISIBLE);
                    mode.setVisibility(View.VISIBLE);
                } else {
                    favorite.setVisibility(View.GONE);
                    mode.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public void hideOrShowBanner(boolean isShow) {
        if (isShow) {
            mServicePointIV.setVisibility(View.VISIBLE);
        } else {
            mServicePointIV.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            // 非地图模式,通过定位当前位置 查询网点列表
            case MsgID.location_service_point:
                BDLocation loc = (BDLocation) msg.obj;
                if (loc == null) {
                    cancelLoadingDialog();
                    showToast("定位失败");
                    return true;
                } else {
                    if (AppConfig.isOtherCity != 0) {
                        mCurrentCityName = AppConfig.getCityName();
                    } else {
                        mCurrentCityName = loc.getCity();
                    }
                    if (isNotEmpty(mCurrentCityName)) {
                        showLoadingDialog("查询中");
                        if (AppConfig.isOtherCity != 0) {
                            AppHttp.getInstance().servicePointSearchByLocation(AppConfig.getOhterCityLng(), AppConfig.getOhterCityLat());
                        } else {
                            AppHttp.getInstance().servicePointSearchByLocation(loc.getLongitude(), loc.getLatitude());
                        }
                    } else {
                        cancelLoadingDialog();
                        showToast("定位失败");
                    }
                }
                return true;
            // 按照经纬度查询网点
            case MsgID.service_point_search_by_location:
                cancelLoadingDialog();
                final AppResult<ServicePointBean> result = JSON.parseList(msg.obj, ServicePointBean.class);
                if (result.isSuccess()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter = new ServicePointAdapter(getContext(), result.getResultList(), mListView);
                            mAdapter.setEmptyHitStr("你附近没有投递点呢，去地图找找吧！");
                            mListView.setAdapter(mAdapter);
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 101 && resultCode == RESULT_OK) {
            ArrayList<ServicePointBean> items = (ArrayList<ServicePointBean>) data.getSerializableExtra("items");
            mAdapter.setItems(items, true);
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
